package service

import (
	"awesomeProject/app/model"
	"awesomeProject/app/response"
	"awesomeProject/global"
	"context"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

type PurchaseTypesSrv struct{}

func (r *PurchaseTypesSrv) All() response.Resp {
	var purchase_types []model.PurchaseTypesMongo

	if(model.Purchase{}.DbType() == "mongodb") {
		filter := bson.D{}
		match := bson.D{{"$match", filter}}
		sort := bson.D{{"$sort", bson.M{"type_id": 1}}}
		cursor, err := global.MongoDB.Collection(model.PurchaseTypesMongo{}.TableName()).Aggregate(context.TODO(), mongo.Pipeline{match, sort})
		if err = cursor.All(context.TODO(), &purchase_types); err != nil {
			return response.ErrQuery.SetMsg(err.Error())
		}
	} else {
		where := "1=1"
		err := global.Gorm.Where(where).Find(&purchase_types).Error
		if err != nil {
			return response.ErrQuery
		}
	}
	return response.OK.WithData(purchase_types)
}
